/**
 * Created by hooshim on 2014-12-04.
 */

var G_grid01 = "";
var G_grid02 = "";
var G_page = 1;

$(document).ready(function() {
    fnGetSubMenus();
    fnSetCurMenu(6);
    //fnGetLeftMenus('5');
    $("#_curMenu p").html("매입관리");

    $("#planDt").datepicker(G_calendar);
    $("#bankDt").datepicker(G_calendar);
    $("#actionDt").datepicker(G_calendar);

    var bodyWidth = $('body').innerWidth();
    bodyWidth = parseInt(bodyWidth,10) - 170;
    $('#_bodyRight').css('width', bodyWidth + 'px');
    var gSubMenus = fnGetSubMenus();
    var gScreenHeight = parseInt($(window).height(),10) - 120;
    //$("#_bodyLeft").css("height",gScreenHeight);
    $("#_bodyAll").css("height",gScreenHeight-58);
    gScreenHeight = parseInt(gScreenHeight,10) - 140;

    fnSetGrid01(gScreenHeight);
    fnGetList01(1);
    fnSetGrid02();
});

var fnSetGrid01 = function(h) {

    var grdCd = "grid01";

    var widths = [70, 70, 150, 80, 50, 80, 50, 250, 120];
    var titles = ['등록일', '지급예정', '거래처', '금액', '지급일', '지급액', '통장일', '내역', 'B/L'];
    var styles = ['text-align:center;', 'text-align:center;', 'text-align:left;', 'text-align:right;', 'text-align:center;', 'text-align:right;color:#CC0000;', 'text-align:center;', 'text-align:left;', 'text-align:center;'];

    var gInfo = new Object();
    gInfo["grdCd"] = "grid01";
    gInfo["title"] = "매입관리";
    gInfo["widths"] = widths;
    gInfo["titles"] = titles;
    gInfo["styles"] = styles;
    gInfo["function"] = "fnShowPayment";
    gInfo["fullWidth"] = "660";
    gInfo["fullHeight"] = h;
    gInfo["fix"] = "3";

    G_grid01 = JSON.stringify(gInfo);

    fnMkGridInit(G_grid01);
}

var fnSetGrid02 = function() {

    var grdCd = "grid02";

    var widths = [102, 75, 75, 75];
    var titles = ['거래처', '금액', '미지급금', '지급총액'];
    var styles = ['text-align:left;', 'text-align:right;','text-align:right;color:#0000CC;','text-align:right;color:#CC0000;'];

    var gInfo = new Object();
    gInfo["grdCd"] = grdCd;
    gInfo["title"] = "거래처별 매입";
    gInfo["widths"] = widths;
    gInfo["titles"] = titles;
    gInfo["styles"] = styles;
    //gInfo["function"] = "fnShowPayment";
    gInfo["fullWidth"] = "286";
    gInfo["fullHeight"] = "280";
    gInfo["fix"] = "1";

    G_grid02 = JSON.stringify(gInfo);

    fnMkGridInit(G_grid02);
}

var fnGetList = function() {

    if(event.keyCode != "13")
    {
        return; // 엔터일때만 수행
    }
    fnGetList01(1);
}

var fnGetList01 = function(page) {
    fnMkGridLoad(G_grid01);
    if (page) G_page = page;
    else page = G_page;
    var param  = "findCompany="+$("#findComapny").val();
    var valUrl = "./action/payment/list.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){

            fnMkGrid(G_grid01,data);
            var sum = data.SUM.TBLPAYMENT[0];
            var sumAmt = sum.SUMAMT;
            var sumPayAmt = sum.SUMPAYAMT;
            $("#sumAmt p").html(sumAmt + ' <span>|</span>');
            $("#sumPayAmt p").html(sumPayAmt + ' <span>|</span>');
            /*
             $("#table_body").html(list);
             $("#total_rows").html(data.TOTAL_ROW_COUNT);
             var paging = fnPaging('fnGetList', data.TOTAL_ROW_COUNT, page, 10);
             var pages = paging['prev'] + paging['pager'] + paging['next'];
             // alert(pages);
             $("#pager").html(pages);
             */
             fnGetList02(1);
        },
        error: function(data){
            alert('Error!!');
            alert(data.responseText);
        }
    });
}

var fnGetList02 = function(page) {
    fnMkGridLoad(G_grid02);
    if (page) G_page = page;
    else page = G_page;
    var param  = "findCompany="+$("#findComapny").val();
    var valUrl = "./action/payment/listCompanySum.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){

            fnMkGrid(G_grid02,data);
            /*
             $("#table_body").html(list);
             $("#total_rows").html(data.TOTAL_ROW_COUNT);
             var paging = fnPaging('fnGetList', data.TOTAL_ROW_COUNT, page, 10);
             var pages = paging['prev'] + paging['pager'] + paging['next'];
             // alert(pages);
             $("#pager").html(pages);
             */
        },
        error: function(data){
            alert('Error!!');
            alert(data.responseText);
        }
    });
}

var fnShowPayment = function(payCds) {
    var divPayCd = payCds.split("^");
    var param  = "payCd="+divPayCd[0];
    param  += "&paySeq="+divPayCd[1];
    var valUrl = "./action/payment/view.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){
            fnReset();

            var dataIncome = data.TBLPAYMENT[0];

            $("#payCd").val(dataIncome.PAYCD);
            $("#billCd").val(dataIncome.BILLCD);
            $("#article").val(dataIncome.ARTICLE);
            $("#planDt").val(dataIncome.PLANDT);
            $("#amount").val(dataIncome.AMOUNT);
            $("#orgAmount").val(dataIncome.ORGAMOUNT);
            $("#tax").val(dataIncome.TAX);
            //$("#memo").val(dataIncome.MEMO);
            $("#flag").val(dataIncome.FLAG);
            if (data.PAYRESULT != undefined) {
                var dataIncResult = data.PAYRESULT.TBLPAYRESULT[0];
                $("#payAmount").val(dataIncResult.PAYAMOUNT);
                $("#actionDt").val(dataIncResult.ACTIONDT);
                $("#bankDt").val(dataIncResult.BANKDT);
                $("#paySeq").val(dataIncResult.PAYSEQ);
                $("#payMemo").val(dataIncResult.PAYMEMO);
                $("#payFlag").val(dataIncResult.PAYFLAG);
            }
            setTimeout("fnComboCompany('','"+dataIncome.COMPCD+"','compCd','skip');",500);
        },
        error: function(request,status,error){
            alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
        }
    });
}

var fnSave = function() {
    if (!$("#article").val()) {
        alert("내역을 바르게 입력해 주세요.");
        $("#article").focus();
        return;
    }
    if (!$("#amount").val()) {
        alert("금액을 바르게 입력해 주세요.");
        $("#amount").focus();
        return;
    }

    var param = "payCd=" + $("#payCd").val();
    param += "&billCd=" + $("#billCd").val();
    param += "&planDt=" + $("#planDt").val();
    param += "&article=" + $("#article").val();
    param += "&amount=" + $("#amount").val().split(',').join('');
    param += "&orgAmount=" + $("#orgAmount").val().split(',').join('');
    param += "&tax=" + $("#tax").val().split(',').join('');
    param += "&compCd=" + $("#compCd").val();
    param += "&flag=" + $("#flag").val();
    param += "&paySeq=" + $("#paySeq").val();
    param += "&payAmount=" + $("#payAmount").val().split(',').join('');
    param += "&actionDt=" + $("#actionDt").val();
    param += "&bankDt=" + $("#bankDt").val();
    param += "&payMemo=" + $("#payMemo").val();
    param += "&payFlag=" + $("#payFlag").val();

    var valUrl = "./action/payment/save.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){
            if (data.rsStatus == "Success") {
                alert(data.rsStr);
                fnReset();
                fnGetList01();
                fnGetList02();
            }
            else {
                alert("저장 실패");
            }
        },
        error: function(request,status,error){
            alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
        }
    });
}

var fnReset = function() {
    $("#payCd").val('');
    $("#billCd").val('');
    //$("#incomeDt").val('');
    $("#article").val('');
    $("#amount").val('');
    $("#orgAmount").val('');
    $("#tax").val('');
    $("#compNm").val('');
    $("#compCd").val('');
    //$("#memo").val('');
    $("#flag").val('Y');

    fnAddPayment();
}

var fnAddPayment = function() {
    $("#payAmount").val('');
    //$("#actionDt").val('');
    //$("#bankDt").val('');
    $("#paySeq").val('');
    $("#payMemo").val('');
    $("#payFlag").val('Y');
}

var fnSetPayResult = function(m) {
    if (m == "all") {
        $("#payAmount").val($("#amount").val());
        $("#bankDt").val($("#planDt").val());
        $("#actionDt").val($("#planDt").val());
    }
    else
    {
        $("#payAmount").val($("#amount").val());
    }
}